package com.project.service.impl;

import com.project.bean.CutPageBean;
import com.project.bean.StudentBean;
import com.project.mapper.IStudentMapper;
import com.project.service.IStudentService;
import org.apache.ibatis.session.SqlSession;

import java.util.List;

public class StudentServiceImpl  extends BaseService implements IStudentService {

    @Override
    public void add(StudentBean student) {
        SqlSession session = this.getSession();
        IStudentMapper mapper = session.getMapper(IStudentMapper.class);
        mapper.add(student);
        session.commit();
        session.close();
    }

    @Override
    public void del(Integer id) {
        SqlSession session = this.getSession();
        IStudentMapper mapper = session.getMapper(IStudentMapper.class);
        mapper.del(id);
        session.commit();
        session.close();
    }

    @Override
    public void update(Integer studentId, Integer roomId) {
        SqlSession session = this.getSession();
        IStudentMapper mapper = session.getMapper(IStudentMapper.class);
        mapper.update(studentId,roomId);
        session.commit();
        session.close();
    }

    @Override
    public StudentBean getById(Integer id) {
        SqlSession session = this.getSession();
        IStudentMapper mapper = session.getMapper(IStudentMapper.class);
        StudentBean student = mapper.getById(id);
        session.close();
        return student;
    }

    @Override
    public CutPageBean<StudentBean> cutByItem(int pageNO, String name, String className, String roomAddr) {
        CutPageBean<StudentBean> cutPageBean = new CutPageBean<>();
        SqlSession session = this.getSession();
        IStudentMapper mapper = session.getMapper(IStudentMapper.class);
        List<StudentBean>list = mapper.cutByItemList((pageNO-1)*CutPageBean.PAGESIZE,CutPageBean.PAGESIZE,name,className,roomAddr);
        cutPageBean.setList(list);
        cutPageBean.setCount(mapper.getTotalByItem(name,className,roomAddr));
        session.close();
        return cutPageBean;
    }
}
